using System.Collections.Generic;
using System.Linq;
using Baik.Interface.Execution;
using Baik.Interface.Expression;
using Baik.Interface.Framework;

namespace Baik.Expression
{
    public class ObjectLiteral : Expression,  IObjectLiteral
    {
        public ObjectLiteral(IInjector inject) : base(inject)
        {
            
        }
        
        public override IObject Evaluate()
        {

            var rtn = Inject.Instantiate<IObject>();
            rtn.DeclarationScope = EvaluationScope;
            Assigments.ToList().ForEach(a => a.Evaluate(rtn));
            return rtn;
        }
        private string partstring()
        {
            var lst = Assigments as IAssigment[] ?? Assigments.ToArray();
            var sw = new System.IO.StringWriter();
            for (int i = 0; i < lst.Length; i++)
            {
                sw.Write(lst[i].AsString() + ";");
            }
            return sw.ToString();
        }
        public string AsString()
        {
            return "{" + partstring() + "}";
        }
        public IEnumerable<IAssigment> Assigments { get; set; }
        
    }
}